<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/18
 * Time: 11:16
 */

namespace App\Http\Controllers\Admin;

use App\Model\RecordBonus;
use App\Model\RecordWithdraw;
use App\Model\ShopGoods;
use App\Model\ShopGoodsImg;
use App\Model\Tutor;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Model\Members;
use App\Model\RecordrRecharge;
use App\Model\RecordTransaction;
use App\Model\ShopOrder;
use Session;
class IndexController extends BaseController
{
    /**
     * 后台首页
     */
    public function index()
    {

        return view('Admin/index');
    }
    /**
     * 系统设置
     */
    public function settings()
    {
        return view('Admin/system');
    }

    public function doIndex()
    {
         $ShopOrder = new ShopOrder();
         $RecordBonus = new RecordBonus();
         $RecordRecharge = new RecordrRecharge();
        //会员数据
        $membersNum = Members::count();
        $membersModel = new Members();
        $date = date('Y-m-d',time());
        $yDate = date("Y-m-d",strtotime("-1 day"));
        //今日新增会员
        $memberSum = $membersModel->where('created_at','like',$date.'%')->count();
        //今日订单
        $dailyCount = $ShopOrder->where('paytime','like',$date.'%')->where('status','>',0)->count();
        //今日收入
        $recordSum = $ShopOrder->where('paytime','like',$date.'%')->where('status','>',0)->sum('price');
         //昨日参与积分分润
        $credit2Sum = $RecordBonus->where('created_at','like',$yDate.'%')->where('type','=',1)->sum('money');
        //昨日分享积分分润
        $credit5Sum = $RecordBonus->where('created_at','like',$yDate.'%')->where('type','=',2)->sum('money');
        //成交订单总数量
        $orderNum = ShopOrder::where('status','>',0)->count();
       //成交订单总金额
        $orderMoney = ShopOrder::where('status','>',0)->sum('price');
        //积分总和
        $credit1Num = Members::sum('credit1');
        //参与积分总和
        $credit2Num = Members::sum('credit2');
        //冻结积分总和
        $credit3Num = Members::sum('credit3');
        //分享积分总和
        $credit5Num = Members::sum('credit5');
        //全盘分润
        $RebateNum = RecordBonus::sum('money');
        //总充值
        $rechargeNum = RecordTransaction::where('money','>',0)->where('type','=',4)->sum('money');
        //今日充值
        $dayRecharge = RecordTransaction::where('created_at','like',$date.'%')->where('money','>',0)->where('type','=',4)->sum('money');
        //成功提现总额
        $moneyData =  RecordWithdraw::where('status',1)->sum('money');
        $recordData =  RecordWithdraw::where('status',0)
            ->join('members', 'record_withdraw.uid', '=', 'members.uid')
//            ->join('bank_card','record_withdraw.bank_id' ,'=','bank_card.id')
            ->select('members.user_name','record_withdraw.money','account_name','collection_number')
            ->get();
        //待处理提现数量
        $recorNum = RecordWithdraw::where('status',0)->count();
        //商品销售排行前三
        $goodsSale = ShopGoods::where('type', 1)
            ->selectRaw('id,goodsname,goods_thumb,sold')
            ->groupBy('id')
            ->orderBy('sold','desc')
            ->limit(3)
            ->get()
            ->toArray();
        return [
            'code' =>1,
            'memberNum'=>$membersNum,//全部会员
            'orderNum'=>$orderNum,//成交订单总数量
            'orderMoney'=>$orderMoney,//成交订单总金额
            'RebateNum' =>$RebateNum,//全盘分润
            'credit1Num'=>$credit1Num,//积分总和
            'credit5Num'=>$credit5Num,//分享总和
            'credit2Num'=>$credit2Num,//参与总和
            'credit3Num'=>$credit3Num,//参与总和
            'dailyCount' =>$dailyCount,//今日订单
            'recordSum' =>$recordSum,//今日收入
            'goodsSale' =>$goodsSale,//销量前三商品
            'recordData'=>$recordData,//待处理提现
            'recorNum'=>$recorNum,//待处理提现数量
            'credit2Sum'=>$credit2Sum,//今日参与分润
            'credit5Sum'=>$credit5Sum,//今日分享分润
            'moneyData'=>$moneyData,//成功提现总金额
            'memberSum'=>$memberSum,//今日新增会员
            'rechargeNum'=>$rechargeNum,//总充值
            'dayRecharge'=>$dayRecharge,//今日充值
        ];

    }
    /**
     * 导师
     */
    public  function tutorIndex(){

        return view('Admin/tutor/index');
    }
    /**
     *案例数据接口
     */
    public function tutorList(Request $request)
    {
        $data = $request->all();
        $tutorModel = new Tutor();
        switch ($data['class']) {
            case 'select':
                $video = $tutorModel->paginate(15);
                if ($video) {
                    return ['code' => 1, 'data' => $video];
                } else {
                    return ['code' => 0, 'data' => '暂无导师'];
                }
                break;
            case 'classify':
                $classify = CaseClassify::select('id', 'name')->get()->toArray();
//                var_dump($classify);exit;
                if ($classify) {
                    return ['code' => 1, 'data' => $classify];
                } else {
                    return ['code' => 0, 'data' => '暂无分类'];
                }
                break;
            case 'find':
                if (isset($data['id'])) {
                    $detail = $tutorModel->where('id', $data['id'])->first();
                    if ($detail) {
                        return ['code' => 1, 'data' => $detail];
                    } else {
                        return ['code' => 0, 'data' => '案例不存在'];
                    }
                }
                break;
            case 'add':
                if (empty($data['case']['cid'])) {
                    return ['code' => 0, 'data' => '案例分类不能为空'];
                }
                if (empty($data['case']['case_name'])) {
                    return ['code' => 0, 'data' => '案例名称不能为空'];
                }
//                if (empty($data['case']['video'])){
//                    return ['code' => 0,'data' => '请上传案例视频'];
//                }
                if (empty($data['case']['detail'])) {
                    return ['code' => 0, 'data' => '请添加案例详情'];
                }
                if (empty($data['case']['pic'])) {
                    return ['code' => 0, 'data' => '请上传案例封面图'];
                }
                if($data['case']['id']){
                    $res = $tutorModel->where('id', $data['case']['id'])->update($data['case']);
                }else{
                    $res = $tutorModel->create($data['case']);
                }

                if ($res) {
                    return ['code' => 1, 'data' => ' 设置成功'];
                } else {
                    return ['code' => 0, 'data' => '设置失败'];
                }
                break;
            case 'delete':
                if (isset($data['id'])) {
                    $res = $videoCaseModel->where('id', $data['id'])->delete();
                    if ($res) {
                        return ['code' => 1, 'data' => '删除成功'];
                    } else {
                        return ['code' => 0, 'data' => '删除失败'];
                    }
                }
                break;
        }
    }
}